package com.peas.platform.module.icims.base.mdm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.mdm.domain.GovernmentDomain;
import com.peas.platform.module.icims.base.mdm.dto.GovernmentDto;
import com.peas.platform.module.icims.base.mdm.service.GovernmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GovernmentServiceImpl extends BaseServiceImpl<GovernmentDto> implements GovernmentService {

    @Resource
    private GovernmentDomain governmentDomain;

    @Override
    public BaseDomain<GovernmentDto, ? extends BasePo> getDomain() {
        return governmentDomain;
    }

    @Override
    public List<GovernmentDto> queryGovernmentAndSubs(Long governmentId) {
        List<GovernmentDto> list = new ArrayList<>();
        GovernmentDto root = this.getById(governmentId);
        this.addGovernmentAndSubs(root, list);
        return list;
    }

    private void addGovernmentAndSubs(GovernmentDto dto, List<GovernmentDto> list) {
        if (dto == null) {
            return;
        }
        list.add(dto);
        List<GovernmentDto> gds = this.listByField("parentId", dto.getId());
        CollUtil.forEach(gds, (CollUtil.Consumer<GovernmentDto>) (value, index) -> addGovernmentAndSubs(value, list));
    }

}
